//
//  CYOrderCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/10/16.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYOrderCollectionCell.h"
#import "CYOrderCell.h"
#import "CYOrderCollectionCellSubView.h"
#define ROWHEIGHT 70
@interface CYOrderCollectionCell ()<UITableViewDelegate, UITableViewDataSource, CYOrderCollectionCellSubViewDelegate>
@property (nonatomic, assign) int page;//页码
@property (nonatomic, assign) int rows;//每夜显示行数
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CYOrderCollectionCellSubView *subView;
@property (nonatomic,strong) NSURLSessionTask *task;
@property (nonatomic, strong) CYTotalOrderModel *model;
@end
@implementation CYOrderCollectionCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _status = 100;
        _rows = ceil((SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT - 44) / ROWHEIGHT) + 1;
    }
    return self;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.mj_header = [CYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDown)];//下拉刷新
        _tableView.mj_footer = [CYRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(refreshUp)];//上拉加载
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0,0.0,0.0,0.0));
        }];
    }
    return _tableView;
}

- (CYOrderCollectionCellSubView *)subView{
    if (!_subView) {
        _subView = [[CYOrderCollectionCellSubView alloc]init];
        _subView.delegate = self;
        [self addSubview:_subView];
        
        [_subView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0,0.0,0.0,0.0));
        }];
    }
    return _subView;
}

- (void)setStatus:(int)status{
    if (_status != status)
        _status = status;
    [self https];
    [self shake];
}
- (void)setModel:(CYTotalOrderModel *)model{
    if (_model != model) {
        if (_model.bets_data.count < 1)
            _model = model;
        else if(_model.bets_data.count/_rows < model.total_page && _page > 1 && model.bets_data.count > 0)
            [_model.bets_data addObjectsFromArray:model.bets_data];
        else if (_page == 1 && model.bets_data.count > 0)
            [_model.bets_data replaceObjectsInRange:NSMakeRange(0, MIN(_rows, model.bets_data.count)) withObjectsFromArray:model.bets_data];
        self.tableView.hidden = (_model.total_page > 0)?NO:YES;
        self.subView.hidden = (_model.total_page > 0)?YES:NO;
        [self.tableView reloadData];
    }
}

//下拉刷新
- (void)refreshDown{
    _page = 1;
    [self https];
}

//上拉加载更多
- (void)refreshUp{
    _page = (int)_model.bets_data.count / _rows;
    _page++;
    [self https];
}

//摇一摇
- (void)shake{
    if (_subView.hidden == NO && _subView) {
        [_subView shake];
    }
}

#pragma - mark - UITableViewDataSource method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _model.bets_data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"CYOrderCell";
    CYOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
        cell = [[CYOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    CYOrderModel *subOrderModel = _model.bets_data[indexPath.row];
    cell.name = subOrderModel.cz_name;//彩种名字
    cell.date = subOrderModel.action_no;//彩种日期
    cell.bet_way = subOrderModel.wf_name;//投注方式
    cell.bet_money = subOrderModel.bet_bonus;//投注金额
    cell.status = subOrderModel.zhuangtai;//注单状态
    cell.money = subOrderModel.bonus;//中奖金额
    return cell;
    
}
#pragma - mark - UITableViewDelegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ROWHEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([_delegate respondsToSelector:@selector(selectIndex:ID:)]){
        CYOrderModel *subOrderModel  = _model.bets_data[indexPath.row];
        [_delegate selectIndex:indexPath ID:subOrderModel.ID];
    }
}

#pragma - mark - CYOrderCollectionCellSubViewDelegate method
- (void)orderBtnDidSelected:(NSMutableArray *)array{
    if ([_delegate respondsToSelector:@selector(selectOrderBtnWithArray:)])
        [_delegate selectOrderBtnWithArray:array];
}

- (void)https{
    
    NSDictionary *para = @{@"username":[CYUser shareUser].username ,
                           @"token":[CYUser shareUser].token,
                           @"page":@(_page),
                           @"number":@(_rows),
                           @"status":@(_status)};
    
    self.task = [CYHttps POST:API_order parameters:para showHUD:YES progress:nil success:^(id data) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        
        if ([data isKindOfClass:[NSDictionary class]]){
            self.model = [CYTotalOrderModel mj_objectWithKeyValues:data];
        }
        
    } failure:^(id string) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}
@end
